//
//  MainViewController.m
//  BeanSproutEDU
//
//  Created by douya on 15/1/6.
//  Copyright (c) 2015年 douya. All rights reserved.
//

#import "MainViewController.h"
#import "DouyaPlayerViewController.h"
#import "DrawerView.h"
#import "MainView.h"
#import "MyCourseView.h"
#import "ChargeAndBalanceView.h"
#import "CourseDetailsViewController.h"
#import "PersonDataView.h"

#import "HttpManager.h"
#import "MainModel.h"
#import "VideoModel.h"
#import "DouyaIndicator.h"
#import "DYViewController.h"
@interface MainViewController ()

@end

@implementation MainViewController

-(void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"closePlayer" object:nil];
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"MainView"];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"MainView"];
}



- (void)viewDidLoad {
    [super viewDidLoad];

    [self setNavigationBar];
    
    [self addNotification];
    
    //从服务器请求数据
    [self requestDataFromWeb];
   
   
    
}

-(void)addNotification
{
    //点击抽屉的单元格时抽屉的view发给controller的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(change:) name:@"changeView" object:nil];
    
    //进入到课程详细时的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushCourseDetail) name:@"pushCourseDetailVC" object:nil];
    //程序进入后台的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
    //一键播放的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(beginPlay:) name:@"beginPlay" object:nil];
    //播放精品课程的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(BCPlay:) name:@"BCPlay" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(aginRequestDataFromWeb) name:@"reloadNewData" object:nil];
}
-(void)aginRequestDataFromWeb
{
    NSMutableDictionary *videoDict=[NSMutableDictionary dictionary];
    
    
    [HttpManager getMainInfoWithBlock:^(NSMutableArray *array,BOOL finished)
     {
         if (finished)
         {
             MainModel *mModel=array[0];
             [videoDict setObject:mModel.MSGArray forKey:@"New"];
             
             
             [HttpManager getHotVideoWithBlock:^(NSMutableArray *array,BOOL finished)
              {
                  if (finished)
                  {
                      MainModel *mModel=array[0];
                      [videoDict setObject:mModel.MSGArray forKey:@"Hot"];
                      
                      if ([videoDict objectForKey:@"Hot"])
                      {
                          [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:videoDict];//通知界面的tableview更新数据
                          
                      }
                      else
                      {
                          
                      }
                      
                  }
              }];
             
         }
     }];

}
-(void)setNavigationBar
{
    self.navigationItem.title=@"豆芽";
    self.navigationController.navigationBar.titleTextAttributes=[NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];//设置title的颜色
    self.navigationController.navigationBar.barTintColor=DOUYACOLOR;//设置bar的颜色
    self.navigationController.navigationBar.tintColor=[UIColor whiteColor];//设置bar上面按钮的颜色
    
    //菜单栏按钮
    _menuBI=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"douya1.png"] style:UIBarButtonItemStyleDone target:self action:@selector(menuBIClick)];
    _menuBI.enabled=NO;
    _menuBI.tag=250;
    self.navigationItem.leftBarButtonItem=_menuBI;
}
//从服务器请求数据
-(void)requestDataFromWeb
{
    DouyaIndicator *indicator=[[DouyaIndicator alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT)];
    [indicator setLoadText:@"加载中 . . ."];
    [self.view addSubview:indicator];
    [indicator beginAnimation];
    
    NSMutableDictionary *videoDict=[NSMutableDictionary dictionary];
    
    
    [HttpManager getMainInfoWithBlock:^(NSMutableArray *array,BOOL finished)
     {
         if (finished)
         {
             MainModel *mModel=array[0];
             [videoDict setObject:mModel.MSGArray forKey:@"New"];
             
             
             [HttpManager getHotVideoWithBlock:^(NSMutableArray *array,BOOL finished)
              {
                  if (finished)
                  {
                      MainModel *mModel=array[0];
                      [videoDict setObject:mModel.MSGArray forKey:@"Hot"];
                      
                      if ([videoDict objectForKey:@"Hot"])
                      {
                          [self createAllView:videoDict];
                          [indicator stopAnimationWithLoadText:@"加载成功" withType:YES];
                      }
                      else
                      {
                          [indicator stopAnimationWithLoadText:@"加载失败" withType:NO];
                      }
                      
                  }
              }];

         }
     }];
    
    
    
}

-(void)createAllView:(NSDictionary *)dict
{
    _menuBI.enabled=YES;
    //创建抽屉菜单
    DrawerView *drawer=[DrawerView createDrawer];
    
    drawer.frame=CGRectMake(0, 64, 200,SCREENHEIGHT-64);
    
    [self.view addSubview:drawer];
    
    //创建此UIView是为了加载以下的视图  方便统一的移动
    _bigView=[[UIView alloc] initWithFrame:CGRectMake(0, 64,SCREENWIDTH, SCREENHEIGHT-64)];
    [self.view addSubview:_bigView];
    
    //1、创建全部课程视图
    _mainView=[[MainView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT)];
   
    _mainView.backgroundColor=[UIColor groupTableViewBackgroundColor];
    [_bigView addSubview:_mainView];
     [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:dict];//通知界面的tableview更新数据
    
    
    //2、创建我的课程视图
    _myCourseView=[[MyCourseView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT)];
    
    _myCourseView.alpha=0;
    [_bigView addSubview:_myCourseView];
    
    //3、创建个人资料视图
    _personDataView=[[PersonDataView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT)];
    //    _personDataView.backgroundColor=[UIColor whiteColor];
    _personDataView.alpha=0;
    [_bigView addSubview:_personDataView];
    
    //4、创建充值/余额视图
    _chargeView=[[ChargeAndBalanceView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT)];
    _chargeView.backgroundColor=[UIColor yellowColor];
    _chargeView.alpha=0;
    [_bigView addSubview:_chargeView];
    
}

//精品课程的播放事件
-(void)BCPlay:(NSNotification *)notification
{
    NSURL *url=(NSURL *)notification.object;
    _BCPlayer=[[DYViewController alloc] initWithContentURL:url];
    [self.navigationController pushViewController:_BCPlayer animated:YES];

}

#pragma mark- 抽屉切换视图
-(void)change:(NSNotification*)notification
{
    
    int note=[notification.object intValue];
    switch (note)
    {
        case 0://全部课程
        {
            [self close];
            self.navigationItem.title=@"豆芽";
            _mainView.alpha=1;
            _personDataView.alpha=0;
            _myCourseView.alpha=0;
            _chargeView.alpha=0;
            NSDictionary *dict=@{@"action":@"allCourse"};
            [MobClick event:@"menuSubBtnClick" attributes:dict];
        }
            break;
        case 1://我的课程
        {
            [self close];
            self.navigationItem.title=@"我的课程";
            _mainView.alpha=0;
            _myCourseView.alpha=1;
            _personDataView.alpha=0;
            _chargeView.alpha=0;
            NSDictionary *dict=@{@"action":@"myCourse"};
            [MobClick event:@"menuSubBtnClick" attributes:dict];
            
        }
            break;
        case 2://个人资料
        {
            [self close];
             self.navigationItem.title=@"个人资料";
            _mainView.alpha=0;
            _myCourseView.alpha=0;
            _personDataView.alpha=1;
            _chargeView.alpha=0;
            NSDictionary *dict=@{@"action":@"personData"};
            [MobClick event:@"menuSubBtnClick" attributes:dict];
        }
            break;
        case 3://余额/充值
        {
            [self close];
             self.navigationItem.title=@"充值/余额";
            _mainView.alpha=0;
            _myCourseView.alpha=0;
            _personDataView.alpha=0;
            _chargeView.alpha=1;
            NSDictionary *dict=@{@"action":@"chargeView"};
            [MobClick event:@"menuSubBtnClick" attributes:dict];
        }
            break;
            
        default:
            break;
    }
}
#pragma mark- 点击个人信息按钮 打开抽屉
//个人信息事件
-(void)menuBIClick
{
    [MobClick event:@"menuBtnClick"];
    _isOpen=!_isOpen;
    if (_isOpen)
    {
        //创建一个覆盖按钮 目的是当打开抽屉时主界面不能被点击
        UIButton *bigBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
        bigBtn.tag=100;//方便后面移除
        bigBtn.backgroundColor=[UIColor clearColor];
        bigBtn.frame=CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
        [bigBtn addTarget:self action:@selector(bigBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [_bigView addSubview:bigBtn];
        
        [UIView animateWithDuration:0.2 animations:^{
            self.navigationController.navigationBar.frame=CGRectMake(200, 20, self.view.frame.size.width, 44);
            _bigView.frame=CGRectMake(200, 64, self.view.frame.size.width, self.view.frame.size.height-64);
        }completion:nil];
    }
    else
    {
        [self close];
    }
    
}
#pragma mark- 透明的覆盖按钮点击方法
-(void)bigBtnClick
{
    [self close];
}
#pragma mark 关闭抽屉方法
-(void)close
{
    _isOpen=NO;//抽屉都关闭了 那状态一定是关闭撒
    
    [UIView animateWithDuration:0.2 animations:^{
        self.navigationController.navigationBar.frame=CGRectMake(0, 20, self.view.frame.size.width, 44);
        _bigView.frame=CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64);
    }completion:^(BOOL finishe){
        [[self.view viewWithTag:100] removeFromSuperview];
    }];
}
#pragma mark-进入到课程详细界面
-(void)pushCourseDetail
{
    CourseDetailsViewController *CDVC=[[CourseDetailsViewController alloc] init];
    CDVC.mediaInfoArray=_vedioArray;
    [self.navigationController pushViewController:CDVC animated:YES];
}

-(void)rightBIClick
{
  
}

-(void)didEnterBackground
{
    [self close];
}


#pragma mark- 一键播放方法
-(void)beginPlay:(NSNotification *)notification
{
    _BCPlayer=nil;
    NSMutableArray *array=(NSMutableArray *)notification.object;
    VideoModel *vModel=array[0];
    _BCPlayer=[[DYViewController alloc] initWithContentURL:[NSURL URLWithString:vModel.video_url]];
    _BCPlayer.videoArray=array;
    [self.navigationController pushViewController:_BCPlayer animated:YES];
  
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation

{
    return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}

- (BOOL)shouldAutorotate

{
    return NO;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
